package com.kadron.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.creator.KItemModelCreator;
import com.kadron.dao.KDiscountDAO;
import com.kadron.dao.KDiscountItemDAO;
import com.kadron.dao.KImageDAO;
import com.kadron.dao.KItemDAO;
import com.kadron.datamodel.KDataModel;
import com.kadron.datamodel.KModel;
import com.kadron.entity.Discount;
import com.kadron.entity.DiscountItem;
import com.kadron.enumeration.DiscountType;
import com.kadron.item.KItemListSearch;
import com.kadron.item.KItemModel;
import com.kadron.manager.KMessageManager;
import com.kadron.provider.KDiscountItemProvider;

@AutoCreate
@Name("kDiscountManager")
@Scope(ScopeType.PAGE)
public class KDiscountManager extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1321559844485841745L;

	private static final Log log = LogFactory.getLog(KDiscountManager.class);
	
	private Discount discount; 
	private KDataModel itemList;
	private KDiscountItemProvider dataProvider;
	private KItemListSearch search;
	private boolean itemPopup;
	private boolean groupPopup;
	private List<KItemModel> items = new ArrayList<KItemModel>();
	
	private Integer kadrons;
	private Integer bonusKadrons;
	private Integer percent;
	
	@In
	private KDiscountItemDAO discountItemDAO;
	@In
	private KDiscountDAO discountDAO;
	@In
	private KItemDAO itemDAO;
	@In
	private KImageDAO imageDAO;
	
	@Create
	public void init() {
		Long discountId = (Long) Component.getInstance("discountId");
		if (discountId == null) {
			discountId = new Long(0);
		}
		if (discountId > 0) {
			discount = (Discount) discountItemDAO.find(discountId, Discount.class);
			kadrons = discount.getKadrons();
			bonusKadrons = discount.getBonusKadrons();
			percent = discount.getPercent();
			loadItems();
		} else {
			discount = new Discount();
		}
	}
	
	private void loadItems() {
		items.clear();
		List<Long> itemIDs = discountItemDAO.getDiscountItemIDs(discount.getId());
		KItemModelCreator creator = new KItemModelCreator(itemDAO, discountItemDAO, imageDAO);
		for (Long itemId : itemIDs) {
			KItemModel kiModel = creator.create(itemId);
			items.add(kiModel);
		}
	}
	
	public void search() {
		if (dataProvider == null) {
			dataProvider = new KDiscountItemProvider(new KItemModelCreator(itemDAO, discountItemDAO, imageDAO),search, discount.getId(), discountItemDAO);			
		} else {
			dataProvider.setSearch(search);
		}
		itemList = new KDataModel(dataProvider);		
	}
	
	public void reset() {
		search = new KItemListSearch(null);
		dataProvider = new KDiscountItemProvider(new KItemModelCreator(itemDAO, discountItemDAO, imageDAO),search, discount.getId(), discountItemDAO);
		itemList = new KDataModel(dataProvider);		
	}
	
	public void closeItemPopup() {
		search = null;
		dataProvider = null;
		itemList = null;
		itemPopup = false;
		loadItems();
	}
	
	public void openItemPopup() {
		reset();
		itemPopup = true;
	}
	
	public String save() {
		if (discount == null 
				|| percent == null
				|| percent <= 0
			) {
			bad(KConst.BAD_THING);
			return null;
		}
		if (log.isDebugEnabled()) {
			log.debug("Save Discount discountId="+discount.getId());
		}
		discount.setType(DiscountType.PERCENT);
		discount.setPercent(percent);
		discountDAO.update(discount);
		good();
		return "discount";
	}
	
	public String saveKadrons() {
		if (discount == null 
				|| kadrons == null
				|| kadrons <= 0
				|| bonusKadrons == null
				|| bonusKadrons <= 0
				) {
			bad(KConst.BAD_THING);
			return null;
		}
		if (log.isDebugEnabled()) {
			log.debug("Save Discount discountId="+discount.getId());
		}
		boolean newobject = discount.getId() == 0;
		discount.setType(DiscountType.KADRONS);
		discount.setKadrons(kadrons);
		discount.setBonusKadrons(bonusKadrons);			
		if (!newobject) {
			discountItemDAO.deleteItems(discount.getId());
		}
		discountDAO.update(discount);
		good();
		return "discount";
	}
	
	public void addItems() {
		if (itemList == null) {
			bad(KConst.BAD_THING);
			return;
		}
		if (log.isDebugEnabled()) {
			log.debug("Add items discountId="+discount.getId());
		}
		for (KModel model : itemList.getList()) {
			KItemModel kiModel = (KItemModel) model;
			if (kiModel.isMarked()) {
				DiscountItem discountItem = new DiscountItem(discount, kiModel.getItem());
				discountItemDAO.update(discountItem);
			}
		}
		discountDAO.update(discount);
		reset();
		good();
	}
	
	public void delete(KItemModel model) {
		if (model == null) {
			bad(KConst.BAD_THING);
			return;
		}
		discountItemDAO.deleteItem(discount.getId(), model.getItem().getId());
		loadItems();
		good();
	}

	public Discount getDiscount() {
		return discount;
	}

	public void setDiscount(Discount discount) {
		this.discount = discount;
	}

	public KDataModel getItemList() {
		return itemList;
	}
	
	public boolean isItemPopup() {
		return itemPopup;
	}

	public void setItemPopup(boolean itemPopup) {
		this.itemPopup = itemPopup;
	}

	public boolean isGroupPopup() {
		return groupPopup;
	}

	public void setGroupPopup(boolean groupPopup) {
		this.groupPopup = groupPopup;
	}

	public KItemListSearch getSearch() {
		return search;
	}

	public List<KItemModel> getItems() {
		return items;
	}

	public Integer getKadrons() {
		return kadrons;
	}

	public void setKadrons(Integer kadrons) {
		this.kadrons = kadrons;
	}

	public Integer getBonusKadrons() {
		return bonusKadrons;
	}

	public void setBonusKadrons(Integer bonusKadrons) {
		this.bonusKadrons = bonusKadrons;
	}

	public Integer getPercent() {
		return percent;
	}

	public void setPercent(Integer percent) {
		this.percent = percent;
	}
	
}
